package com.servlet;

import com.Imp.MerchantMapperImpl;
import com.Imp.StoreMapperImpl;
import com.Imp.UserMapperImpl;
import com.entity.Merchant;
import com.entity.Store;
import com.entity.User;
import com.mapper.MerchantMapper;
import com.mapper.StoreMapper;
import com.mapper.UserMapper;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import java.util.List;


public class StoreController extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        StoreMapper storeMapper = new StoreMapperImpl();
        List<Store> storeList = storeMapper.getAllstores();

        request.setAttribute("storeList", storeList);

        request.getRequestDispatcher("storeList.jsp").forward(request, response);

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        HttpSession session = request.getSession();
        String merchantId = (String) session.getAttribute("username");
        //获取所有登录数据
        String name=(String)request.getParameter("name");
        String address=(String)request.getParameter("address");
        String remark=(String)request.getParameter("remark");
        //根据用户账户获取到商家信息
        StoreMapper storeMapper = new StoreMapperImpl();
        Store store = storeMapper.getStoresBymerchantId(merchantId);
        if(store == null){
            store = new Store();
            store.setName(name);
            store.setAddress(address);
            store.setRemark(remark);
            //将更改后的信息插入数据库中
            response.sendRedirect("myStore.jsp");
        }
        else{
            store.setName(name);
            store.setAddress(address);
            store.setRemark(remark);
            //将更改后的信息插入数据库中
            storeMapper.updateStore(store);
            response.sendRedirect("myStore.jsp");
        }
    }
}
